<?php

require_once 'Core/Controller/Action.php';

class Admin_SiteController extends Core_Controller_Action
{
    /**
     * Initialize the action
     *
     * @return void
     */
    public function init()
    {
        parent::init();

        $this->view->breadcrumbs = array(
           $this->view->getTranslation('Site Settings') => $this->baseUrl . '/admin/site'
        );
    }

    /**
     * The default action
     *
     * Render the main site admin interface
     *
     * @return void
     */
    public function indexAction()
    {
        $form = new Admin_Form_Site();
        $form->setAction($this->baseUrl . '/admin/site/edit');

        $this->view->form = $form;

        $this->view->toolbarLinks['Add to my bookmarks'] = $this->baseUrl . '/admin/index/bookmark'
            . '/url/admin_site'
            . '/label/' . $this->view->getTranslation('Site');
    }

    /**
     * Edit action
     *
     * Update the site settings file
     *
     * @return void
     */
    public function editAction()
    {
        $form = new Admin_Form_Site();
        $form->setAction($this->baseUrl . '/admin/site/edit');

        if ($this->_request->isPost() && $form->isValid($_POST)) {
            $settings = Core_Filter_Post::raw('setting');
            $s = new Model_SiteSettings();
            foreach ($settings as $k => $v) {
                $s->set($k, $v);
/* *****************************************************************************
 * TODO: remove redirector and validate form
 * ****************************************************************************/

/* *****************************************************************************
 * TODO: check allowed charsets for MySQL => VALIDATOR ??
            if ('default_charset' == $k) {
                $v = str_replace('-', '', $v);
                $mdlConfig = new Core_Installer_Config();
                $mdlConfig->loadFile();
                $config = $mdlConfig->get();
                $config->production->database->charset = $v;
                $mdlConfig->save();
            }
 * *****************************************************************************
 * TODO: add Validators for all site meta data => consider using Zend_Form
            if ('default_timezone' == $k) {
                $validator = new Core_Validate_Timezone();
                $_SESSION['timezone0'] = Zend_Debug::dump($v);
                $_SESSION['timezone1'] = Zend_Debug::dump($validator->isValid($v));
            }
 * *****************************************************************************
 */
            }
            $s->save();
            $this->_redirect('admin/site');
        }
        $this->view->form = $form;
    }

    /**
     * Console action
     *
     * The console provides an interface for simple command scripts.
     * those scripts go in library/Core/Command/{script name}
     *
     * @return void
     */
    public function consoleAction()
    {
        //set up a unique id for this session
        $session = new Zend_Session_Namespace('console_session');
        $previousId = $session->id;
        $session->id = md5(time());
        $this->view->consoleSession = $session->id;

        //you must validate that the session ids match
        if ($this->_request->isPost() && !empty($previousId)) {
            $this->view->commandExecuted = true;
            $this->view->command = 'Command: ' . Core_Filter_Post::get('command');
            $this->view->date = time();

            //execute command
            //validate the session

            if (Core_Filter_Post::get('consoleSession') == $previousId) {
                $this->view->lastCommand = Core_Filter_Post::get('command');
                if (Core_Filter_Post::get('runCommand')) {
                   $results = Core_Command::run(Core_Filter_Post::get('command'));
                } else if (Core_Filter_Post::get('getInfo')) {
                    $results = Core_Command::info(Core_Filter_Post::get('command'));
                } else {
                    $results = array('ERROR: invalid request');
                }
            } else {
                $results[] = 'ERROR: invalid session';
            }

            $this->view->results = $results;
        }

        $breadcrumbLabel = $this->view->getTranslation('Site Console');
        $this->view->breadcrumbs[$breadcrumbLabel] = $this->baseUrl . '/admin/site/console';
        $this->view->toolbarLinks = array();
        $this->view->toolbarLinks['Add to my bookmarks'] = $this->baseUrl . '/admin/index/bookmark/url/admin_site_console';

    }

    /**
     * Mail test action
     *
     * @return void
     */
    public function mailTestAction()
    {
        $settings = new Model_SiteSettings();
        $message = new Core_Mail();
        $message->send(
            $settings->get('default_email'),
            array($settings->get('default_email'), $settings->get('default_email_sender')),
            $this->view->getTranslation('Core CMS Test Message'),
            'Test'
        );
        $this->_forward('index');
    }

}